package com.nl.controller.web.controller;


import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageInfo;
import com.nl.controller.common.exception.SmsException;
import com.nl.controller.common.model.Response;
import com.nl.controller.common.util.ResponseUtil;
import com.nl.controller.web.bean.MgrState;
import com.nl.controller.web.bean.SmsMgrView;
import com.nl.controller.web.service.SmsMgrService;
import com.nl.controller.web.util.ExcelUtil;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;


@RestController
@RequestMapping("smsMgrView")
@Slf4j
public class SmsMgrViewController {
    @Autowired
    private SmsMgrService smsMgrService;
    
      
    @ApiOperation(value = "短信项目视图", notes = "短信项目视图")
    @ApiImplicitParams({
    @ApiImplicitParam(name = "flag", value = "查询方式", required = true, dataType = "String"),
    @ApiImplicitParam(name = "cityId", value = "地市编码", required = true, dataType = "String"),
    })
    @RequestMapping("findview")
    public Response findView(
    		@RequestParam(value = "flag", required = true) String flag,
    		@RequestParam(value = "cityId", required = false)String cityId,
            @RequestParam Map<String, Object> paramMap
     ){
    	if(((cityId ==null)||("".equals(cityId)))&&("county".equals(flag)))
    	{
    		throw new SmsException("请选择查询条件，开始查询");
    	}else{
    	 PageInfo<SmsMgrView> pageInfo = smsMgrService.findregionView(flag,cityId);
                     
      /*  log.info("短信项目视图返回数据：{}", pageInfo.getList());
        return ResponseUtil.sucess(pageInfo.getList(), pageInfo.getTotal());*/
        
        JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(pageInfo.getList()));
        log.info("短信项目视图返回数据：{}", jsonArray);
        if(pageInfo.getList().size()==1){
       	 throw new SmsException("无数据");
       }else{
        return ResponseUtil.sucess(jsonArray);
    	}
    	}
    }
    


    @ApiOperation(value = "短信项目视图", notes = "短信项目视图")

    @PostMapping("find")
    public Response find(

    ) {
        List<SmsMgrView> list = smsMgrService.findMgrView();
        log.debug("短信项目视图返回数据：{}", list);
        return ResponseUtil.sucess(list);
    }


    @ApiOperation(value = "短信项目视图状态查询", notes = "短信项目视图状态查询")

    @PostMapping("stateCount")
    public Response stateCount(

    ) {
        List<MgrState> list = smsMgrService.stateCount();
        log.debug("短信项目视图状态查询返回数据：{}", list);
        return ResponseUtil.sucess(list);

    }

    @ApiOperation(value = "短信项目视图状态查询", notes = "短信项目视图状态查询")

    @PostMapping("stateCountAll")
    public Response stateCountAll(
    ) {
        List<MgrState> list = smsMgrService.stateCountAll();
        log.debug("短信项目视图状态查询返回数据：{}", list);
        return ResponseUtil.sucess(list);
    }

    @ApiOperation(value = "导出", notes = "导出")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "flag", value = "查询方式", required = true, dataType = "String"),
            @ApiImplicitParam(name = "cityId", value = "查询方式", required = true, dataType = "String")
    })
    @RequestMapping("download")
    public void export(
            HttpServletRequest request, HttpServletResponse response,
            @RequestParam(value = "flag", required = true) String flag,
            @RequestParam(value = "cityId", required = false) String cityId

    ) {
        JSONArray ja = smsMgrService.download(flag,cityId);//获取业务数据集
        Map<String, String> headMap = new LinkedHashMap<>();
        String title = "";
        if ("city".equals(flag)) {
            headMap.put("cityName", "地市");
            headMap.put("runNum", "在发项目数");
            headMap.put("waitNum", "暂停项目数");
            headMap.put("stopNum", "停止项目数");
            title = "短信视图（按地市）";
        } 
        else if ("county".equals(flag)) {
            title = "短信视图（按区县）";
            headMap.put("countyName", "区县");
            headMap.put("runNum", "在发项目数");
            headMap.put("waitNum", "暂停项目数");
            headMap.put("stopNum", "停止项目数");
        }
        ExcelUtil.downloadExcelFile(title, headMap, ja, response,request);
        System.out.println("共" + ja.size() + "条数据,执行");
    }
}



